package com.ocom.common.request.exposeapi.boczhxy;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;

public class SecretUtil {


    private static Logger log = LoggerFactory.getLogger(SecretUtil.class);

    private static String vector = "AED7966C958C149632180F51EDBC9313";
    private static String publicKey = "30819F300D06092A864886F70D010101050003818D0030818902818100E2723C7C47E70C28AD0DFC58F49E462E8008F985C6E00AEB9C736BA35BB05FC1F2E3D28C7104CD75FEA9429AA8F8ACE76AB9ABD96A66E28576CF6EAC167C62F50F992DB1B54C729747F3BCD5EDEA3E67D649D24EFE67B1731A4F0E860A0A11387B4BD3FF74C95D51809D7BC96340DDA5A50CA24DF47256FF20C1A35CBB2F61290203010001";
    private static String privateKey = "30820277020100300D06092A864886F70D0101010500048202613082025D02010002818100E2723C7C47E70C28AD0DFC58F49E462E8008F985C6E00AEB9C736BA35BB05FC1F2E3D28C7104CD75FEA9429AA8F8ACE76AB9ABD96A66E28576CF6EAC167C62F50F992DB1B54C729747F3BCD5EDEA3E67D649D24EFE67B1731A4F0E860A0A11387B4BD3FF74C95D51809D7BC96340DDA5A50CA24DF47256FF20C1A35CBB2F61290203010001028180599F40D527580875E8D80A31E2CEE993E9D4A37D10D3073F6CF2397DB4024043F0AFBE5B1B37FD615FE2F99B6098DB45D3F59C50364E7604C09B2480337E13638F7C75B56BBF478D2ED09FA2D5EA2C258FFEF7309F7FCD70B94B3126709B4F52B2797CE0F85C420FA3F04058A53D58E3D607D4C96F2C84EC0072AD9715FA6BE9024100FBC2994801F18A50E091D38B2E4F304DFBBD12037EA3379060952DD7A711449E774A5B9B5E7B3178C8FDC5A10EC16CA25292AAF5BF62D48790B3F471B2B6F327024100E64280BBE189D0A4755711E6B6B6881CDA49B968F185757068525C2796BB225465986C76BF94014A19ABA19EA6E8E85D85A4E937E9FDFFF701A08B3B4C337B2F0241009D0080629D83AA0441E7EF0AAEF887613A8A703F69AC13AC9FEEA84602540C7CDE5BAAC13BAC8BEF8D386514C984EA60582A4B1F321949FCB8CAE0B83F0144A5024100BA2A8CA0DB122CF1BAB9620CD22F344F77A1E1D95A64D7F5C132CDB56865F6B49BCBA7EE3EA8B07E33D113840D5903FE113EAB1CFCFFD302E77AE0F7597DCA1B02402B61888DFF3BD66165B464E48CA694B83CFE1E5D72949D98C571652FEE67A553405C1064722D732513CB7B321148DC56624F27A8C55A31A8316EE0256EA8156F";

    public static String encrypt(String plainText,String privateKey,String publicKey) {
//        String plainText = JSON.toJSONString(object);
        log.info("请求原文:" + plainText);
        //1.请求明文生成签名
        String reqSign = RSAService.sign(plainText, privateKey);
        //2.生成cipherText
        SecretStep1 step1 = new SecretStep1();
        step1.setSign(reqSign);
        step1.setPlainText(plainText);
        String step1Json = JSON.toJSONString(step1);
        log.debug("1、签名后报文信息:" + step1Json);
        //3.随机生成Aes秘钥
        String randomKey = KeyValue16();
        log.debug("2、生成Aes密钥明文:" + randomKey);
        //4.根据密钥加密cipherText
        step1Json = AESService.encrypt(step1Json, randomKey, vector);
        log.debug("3、对称加密后报文信息:" + step1Json);
        //5.使用公钥加密密钥
        String skey = RSAService.encrypt(randomKey, publicKey);
        log.debug("4、加密后密钥:" + skey);
        //6.组合最终请求对象
        SecretStep2 step2 = new SecretStep2();
        step2.setCipherText(step1Json);
        step2.setSkey(skey);
        String step2Json = JSON.toJSONString(step2);
        log.info("5、完成加密后报文信息:" + step2Json);
        return step2Json;
    }

    public static String decrypt(String step2Json, String privateKey) {
        log.info("解密前报文信息:" + step2Json);
        //1.反序列化报文
        SecretStep2 step2 = JSONObject.parseObject(step2Json, SecretStep2.class);
        //2.解密对称密钥
        String skey = RSAService.decrypt(step2.getSkey(), privateKey);
        log.info("1、解密后密钥:" + skey);
        String step1Json = AESService.decrypt(step2.getCipherText(), skey, vector);
        //3.解密原报文
        log.info("2、对称解密后密文:" + step1Json);
        //4.反序列化报文
        SecretStep1 secretStep1 = JSONObject.parseObject(step1Json, SecretStep1.class);
        log.info("3、解密后明文及签名:" + JSON.toJSONString(secretStep1));
        return JSON.toJSONString(secretStep1);
    }

    /**
     * 生成16位的密钥
     **/
    public static String KeyValue16() {
        //定义一个字符串（A-Z，a-z，0-9）即62位；
        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        //由Random生成随机数
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        //长度为几就循环几次
        for (int i = 0; i < 16; ++i) {
            //产生0-61的数字
            int number = random.nextInt(62);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }
        return AESService.bytesToHex(sb.toString().getBytes());
    }
}
